package cn.jee.dao;


import cn.jee.domain.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public class StuDao {
  @Autowired
  JdbcTemplate jdbcTemplate;
  public List<Stu>findAll(){
    return jdbcTemplate.query("select * from stu",new BeanPropertyRowMapper<>(Stu.class));
  }
  @Transactional//可以放在方法或者类都可以，变成事务方法
  public void tx(){
    jdbcTemplate.update("insert into stu(username) values('tx-1')");
    //if(true) throw new RuntimeException("test  tx");
    jdbcTemplate.update("insert into stu(username) values('tx-2')");
  }
}
